package com.du.web.websocket.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.server.HttpServerRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2021/7/13
 * @since: 1.8
 */
@RestController
@RequestMapping(value = "test")
@Slf4j
public class TestController {


    @GetMapping(value = "sendData")
    public void sendData(HttpServletRequest request) {
        String token = request.getHeader("x-token");
        log.info("{}", token);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        // your app key
        String appKey = "afbca057dfcf11ebaa3cfa163ef1469d";
        // your app secret
        String appSecret = "93947473fd0d28e8f313f22b4711c7c8";
        // time
        String time = String.valueOf(System.currentTimeMillis());
        // sign
        String sign = SecureUtil.md5(SecureUtil.md5(appKey + time) + appSecret);

        // code
        String code = SecureUtil.md5(UUID.randomUUID().toString() + appKey);

        headers.add("appKey", appKey);
        headers.add("time", time);
        headers.add("sign", sign);
        headers.add("code", code);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        String data = "{\"QXBM\":440303,\n" +
                "\"QXMC\":\"罗湖区\",\n" +
                "\"YYMC\":\"***系统\",\n" +
                "\"YYTGS\":\"***\",\n" +
                "\"XXBM\":123,\n" +
                "\"XXMC\":\"深圳市**学校\",\n" +
                "\"NJMC\":\"一年级\",\n" +
                "\"BJMC\":\"三班\",\n" +
                "\"XJH\":\"230345\",\n" +
                "\"YHBM\":6547,\n" +
                "\"YHXM\":\"王小川\",\n" +
                "\"DLMC\":\"zxt56432\",\n" +
                "\"DLM\":\"zxt56432\",\n" +
                "\"YHLXBM\":\"01\",\n" +
                "\"YHLXMC\":\"学生\",\n" +
                "\"DLIP\":\"219.134.104.255\",\n" +
                "\"SBH\":\"863254036690765\",\n" +
                "\"DLSJ\":\"2020-10-12 11:34:23\"}";
        params.add("data", data);

        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, headers);

        // url
        String url = "http://szys.changyan.cn/zhkt-api/v1/login/loginInfoAccept";

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        log.info("{}", responseEntity.getBody());
    }

}
